package com.weixin.common;

import com.weixin.exception.BusinessException;
import com.weixin.exception.LoginTimeOutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局捕获异常
 *
 * @author lc
 * @email 389125111@qq.com
 * @create 2018-03-12 14:29
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 业务异常
     *
     * @return ResponseResult
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    private ResponseResult businessException(BusinessException e) {
        logger.error(e.getMessage(), e);
        return ResponseResult.business_error(e.getCode(), e.getMessage());
    }

    /**
     * 登录失效
     *
     * @return ResponseResult
     */
    @ExceptionHandler(LoginTimeOutException.class)
    @ResponseBody
    private ResponseResult loginTimeout(LoginTimeOutException e) {
        logger.error(e.getMessage(), e);
        return ResponseResult.login_timeout();
    }

    /**
     * 服务器错误
     *
     * @return ResponseResult
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    private ResponseResult serverError(RuntimeException e) {
        logger.error(e.getMessage(), e);
        return ResponseResult.server_error();
    }

}
